package com.woniu.aiclient.service.impl;

import com.woniu.aiclient.entity.AiMsg;
import com.woniu.aiclient.prompt.POIPrompt;
import com.woniu.aiclient.service.AiPOIService;
import com.woniu.aiclient.service.RecommendedService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import java.util.Map;
@Service
@Slf4j
public class AiPOIServiceImpl implements AiPOIService {
    @Resource
    private ChatClient poiClient;
    @Resource
    private RecommendedService recommendedService;


@Override
public AiMsg getPOIMessage(String location, String input, String citycode, Boolean isKeyWords) {


    Object recommendedPlaces = recommendedService.getRecommendedPlaces(input);

    Map<String,Object> recommendedPlaces1 =(Map<String,Object>) recommendedPlaces;
    String categoryName = (String) recommendedPlaces1.get("category_name");
    Object keywords1 = recommendedPlaces1.get("keywords");

    PromptTemplate promptTemplate = new PromptTemplate(
            "pos:{pos},citycode:{citycode},keywords:{keywords}"
    );
    promptTemplate.add("pos", location);
    promptTemplate.add("citycode", citycode);
    promptTemplate.add("keywords", keywords1);

    if ("General Intent".equals(categoryName)){
        System.out.println("无想去类型的意图: 进入推荐服务模型");
        AiMsg entity = poiClient
                .prompt()
                .advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "17640122174"))
                .system(POIPrompt.POI_NO_KEYS)
                .user(promptTemplate.create().toString())
                .call()
                .entity(new ParameterizedTypeReference<AiMsg>() {
                });
        sort(entity);
        return entity;
    }else if ("Specific Intent".equals(categoryName)) {
        System.out.println("有想去类型的意图: 进入查询服务模型");
        AiMsg entity = poiClient
                .prompt()
                .system(POIPrompt.POI_YES_KEYS)
                .advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "17640122174"))
                .user(promptTemplate.create().toString())
                .call()
                .entity(new ParameterizedTypeReference<AiMsg>() {
                });
        sort(entity);
        return entity;
    }
    else {
        System.err.println("重新选择: 进入查询服务模型");
        AiMsg entity = poiClient
                .prompt()
                .system(POIPrompt.RE_POI)
                .advisors(spec -> spec.param(ChatMemory.CONVERSATION_ID, "17640122174"))
                .user(promptTemplate.create().toString())
                .call()
                .entity(new ParameterizedTypeReference<AiMsg>() {
                });
        sort(entity);
        return entity;
    }

    //排序

}

    private void sort(AiMsg entity) {
        if (entity != null&& entity.getPoiMsg()!= null) {
            // 比较评分
            entity.getPoiMsg().sort((o1, o2) -> {
                if (o1.getRating()!= null && o2.getRating()!= null)
                    return o2.getRating().compareTo(o1.getRating());
                else if (o1.getRating()==null && o2.getRating()==null)
                    return 0;
                else if (o1.getRating() == null)
                    return -1;
                return 1;
            });
        }
    }


//    @Override
//    public AiMsg getPOIMessage(String location, String keywords, String citycode, Boolean isKeyWords) {
//
//        PromptTemplate promptTemplate = new PromptTemplate(
//                " pos:{pos},citycode:{citycode},input:{input} "
//        );
//        promptTemplate.add("pos", location);
//        promptTemplate.add("citycode", citycode);
//        promptTemplate.add("input", keywords);
//
//        AiMsg entity = poiClient
//                .prompt()
//                .system(POIPrompt.switchPrompt(isKeyWords))
//                .user(promptTemplate.create().toString())
//                .call()
//                .entity(new ParameterizedTypeReference<AiMsg>() {
//                });
//        if (entity != null&& entity.getPoiMsg()!= null) {
//         // 比较评分
//            entity.getPoiMsg().sort((o1, o2) -> {
//                if (o1.getRating()!= null && o2.getRating()!= null)
//                    return o2.getRating().compareTo(o1.getRating());
//                else if (o1.getRating()==null && o2.getRating()==null)
//                    return 0;
//                else if (o1.getRating() == null)
//                    return -1;
//                return 1;
//            });
//        }
//        //排序
//        return entity;
//    }

}
